﻿--[[
    @Brief  邮箱帐号登录
    @Author hqzheng
    @Date   2018-06-05
--]]

EMailAccLoginUI = 
{
    sAccPanel  = "pnlMailAcc";

    sAccView   = "pnlAccList";
    sEdtUsrAcc = "pnlAccList\\pnlUsrAcc\\edtUsrAcc";
    sIconUser  = "pnlAccList\\pnlUsrAcc\\icnUsrAcc";
    sLblAccTip = "pnlAccList\\pnlUsrAcc\\lblAccTips";

    sUserPwd   = "pnlUsrPwd\\edtUsrPwd";
    sIconPwd   = "pnlUsrPwd\\icnUsrPwd";

    sCbSavePwd = "cbSavePwd";
    sCbAtLogin = "cbAtLogin";
    sBtnLogin  = "btnLogin";
    sBnQKLogin = "btnQKLogin";
    sPnlLight  = "pnlLight";

    sBtnRegister    = "btnRegister";
    sBtnForgetPSW   = "btnForgetPSW";

    sDefIcon   = "ONEIMAGE:client_login\\nav_tab_icon_email_off.png;Stretch;0;";
    show_view = false;

};

TMailAccLogin =
{
    InitView = function(dwRoot)
        TMailAccLogin.dwRootID = dwRoot;

        comm_center.listen_broadcast("Msg_ClkAccItem", "TMailAccLogin.OnClkAccItem");
        comm_center.listen_broadcast("Msg_AccListShow", "TMailAccLogin.OnAccListShow");
        comm_center.listen_broadcast("Msg_WebPitShow", "TMailAccLogin.OnAccListShow");

        ierd_tgp.tpf_ui.Require("wegame_login\\acc_list.lua");
    end;

    UnInitView = function()
        TAccComboBox.UnInitView();

        TMailAccLogin.bPwdChange = false;       -- 密码是否改变

        -- 界面控件
        TMailAccLogin.dwRootID   = 0;
        TMailAccLogin.pPnlAccUI  = nil;     -- 帐号页面
        TMailAccLogin.pEdtUsrPwd = nil;     -- 密码输入框

        TMailAccLogin.pCbSavePwd = nil;     -- 保存密码
        TMailAccLogin.pCbAtLogin = nil;     -- 自动登录

        TMailAccLogin.pBtnLogin  = nil;      -- 登录
        TMailAccLogin.pBtnForgetPSW   = nil; -- 忘记密码
        TMailAccLogin.pBtnRegister = nil;    -- 注册
        TMailAccLogin.show_view = false;
        TMailAccLogin.ShowTEdit(false);
    end;

    -- 显示登录界面
    ShowView = function()
        if TMailAccLogin.CheckUI() and (nil ~= TMailAccLogin.pPnlAccUI) then
            TMailAccLogin.EnableFade(true);
            TMailAccLogin.show_view = true;

            -- 显示界面
            TMailAccLogin.pPnlAccUI:Show(TRUE);

            -- 显示安全密码框
            TMailAccLogin.RegTASEvent();
            TMailAccLogin.ShowTEdit(true);

            -- 默认焦点设置到编辑框
            TMailAccLogin.pEdtUsrAcc:SetFocus();
        end;
    end;

    -- 隐藏登录界面
    HideView = function()
        TMailAccLogin.show_view = false;
        TMailAccLogin.EnableFade(false);
        TMailAccLogin.StopTimer();

        TMailAccLogin.ShowTEdit(false);
        TMailAccLogin.UnRegTASEvent();

        if nil ~= TMailAccLogin.pPnlAccUI then          
            TMailAccLogin.pPnlAccUI:Show(FALSE);            
        end;
    end;

    EnableFade = function(bEnable)
        TMailAccLogin.bDoAnimate = bEnable;

        if not bEnable then
            TMailAccLogin.StopFade();
        end;
    end;


    -- 检查界面所有控件
    CheckUI = function()
        local dwRoot = ui_utility.get_ctrl_id_by_path(TMailAccLogin.dwRootID, 
                                                      EMailAccLoginUI.sAccPanel);

        TMailAccLogin.pPnlAccUI = ui_utility.get_ctrl_ptr_by_id(dwRoot);

        if nil == TMailAccLogin.pPnlAccUI then
            TGPLog.LogE("MailAccLogin", "Account page is null.");
            return false;
        end;

        TMailAccLogin.pNoQQTips = ui_utility.get_ctrl_ptr_by_path(dwRoot, "ttNoQQTips");

        if nil == TMailAccLogin.pNoQQTips then
            TGPLog.LogE("MailAccLogin", "Get no QQ detected tips control fail.");
            return false;
        end;
        ui_utility.setproperty_by_id(TMailAccLogin.pNoQQTips:GetControlID(), "OnKillFocus",
                                     "TMailAccLogin.OnHideTips");

        -- 帐号输入框
        TMailAccLogin.pEdtUsrAcc  = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EMailAccLoginUI.sEdtUsrAcc);
        TMailAccLogin.nEdtUsrAcc = ui_utility.get_ctrl_id_by_path(dwRoot, 
                                    EMailAccLoginUI.sEdtUsrAcc);       
        if nil == TMailAccLogin.pEdtUsrAcc then
            TGPLog.LogE("MailAccLogin", "Edit user account is null.");
            return false;
        end;

        local   nEditID = TMailAccLogin.pEdtUsrAcc:GetControlID();

        ui_utility.setproperty_by_id(nEditID, "OnEditTextChange", "TMailAccLogin.OnEdtAccChange");
        ui_utility.setproperty_by_id(nEditID, "OnKeyDown", "TMailAccLogin.OnAccKeyDown");
        ui_utility.setproperty_by_id(nEditID, "OnSetFocus", "TMailAccLogin.OnAccSetFocus");
        ui_utility.setproperty_by_id(nEditID, "OnKillFocus", "TMailAccLogin.OnAccKillFocus");

        -- 发光
        TMailAccLogin.pPnlLight = ui_utility.get_ctrl_ptr_by_path(dwRoot, EMailAccLoginUI.sPnlLight);

        if nil == TMailAccLogin.pPnlLight then
            TGPLog.LogE("MailAccLogin", "Get login light button fail.");
            return false;
        end;
        TMailAccLogin.pPnlLight:Show(FALSE);

        TMailAccLogin.pLblAccTip = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EMailAccLoginUI.sLblAccTip);
        TMailAccLogin.nLblAccTip = ui_utility.get_ctrl_id_by_path(dwRoot,
                                    EMailAccLoginUI.sLblAccTip);

        if nil == TMailAccLogin.pLblAccTip then
            TGPLog.LogE("MailAccLogin", "Get account tips fail.");
            return false;
        end;

        if base_utility.CLIENT_TYPE.CHINA_VER == base_utility.get_client_version_type() then
            -- 用户图标
            TMailAccLogin.nPnlUsrIcn = ui_utility.get_ctrl_id_by_path(dwRoot,
                                        EMailAccLoginUI.sIconUser);
            if nil == TMailAccLogin.nPnlUsrIcn or 0 == TMailAccLogin.nPnlUsrIcn then
                TGPLog.LogE("MailAccLogin", "User icon control is null.");
                return false;
            end;

            local   nPwdIcon = ui_utility.get_ctrl_id_by_path(dwRoot, 
                                    EMailAccLoginUI.sIconPwd);

            if nil == nPwdIcon or 0 == nPwdIcon then
                TGPLog.LogE("MailAccLogin", "Password icon control is null.");
                return false;
            end;

            ui_utility.setproperty_by_id(nPwdIcon, "OnMouseHover", "TMailAccLogin.OnEnterPwdIcon");
            ui_utility.setproperty_by_id(nPwdIcon, "OnMouseLeave", "TMailAccLogin.OnLeavePwdIcon");
        end

        -- 帐号列表
        local   nTempID = ui_utility.get_ctrl_id_by_path(dwRoot, EMailAccLoginUI.sAccView);
        if not TAccComboBox.InitView(nTempID, TLoginModule.LOGIN_ACCOUNT_TYPE.KMailLogin) then
            TGPLog.LogE("MailAccLogin", "Account list is null.");
            return false;
        end;

        -- 密码输入框
        TMailAccLogin.pEdtUsrPwd = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EMailAccLoginUI.sUserPwd);
        if nil == TMailAccLogin.pEdtUsrPwd then
            TGPLog.LogE("MailAccLogin", "Password control is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TMailAccLogin.pEdtUsrPwd:GetControlID(),
                                    "OnEditTextChange", "TMailAccLogin.OnPwdEdtChange");


        -- 保存密码
        TMailAccLogin.pCbSavePwd = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EMailAccLoginUI.sCbSavePwd);
        if nil == TMailAccLogin.pCbSavePwd then
            TGPLog.LogE("MailAccLogin", "Save password control is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TMailAccLogin.pCbSavePwd:GetControlID(), 
                                     "OnUnSignaled", "TMailAccLogin.OnSavePwdUnCheck");
        if TLoginModule.IsNetBar() then
            TMailAccLogin.pCbSavePwd:Show(FALSE);
        else
            TMailAccLogin.pCbSavePwd:Show(TRUE);
        end
        -- 自动登录
        TMailAccLogin.pCbAtLogin = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EMailAccLoginUI.sCbAtLogin);
        if nil == TMailAccLogin.pCbAtLogin then
            TGPLog.LogE("MailAccLogin", "Auto-login control is null.");
            return false;
        end;
        ui_utility.setproperty_by_id(TMailAccLogin.pCbAtLogin:GetControlID(), 
                                     "OnSignaled", "TMailAccLogin.OnAtLoginCheck");
        
        if TLoginModule.IsNetBar() then
            TMailAccLogin.pCbAtLogin:Show(FALSE);
        else
            TMailAccLogin.pCbAtLogin:Show(TRUE);
        end

        -- 登录按钮
        TMailAccLogin.pBtnLogin = ui_utility.get_ctrl_ptr_by_path(dwRoot, 
                                    EMailAccLoginUI.sBtnLogin);
        if nil == TMailAccLogin.pBtnLogin then
            TGPLog.LogE("MailAccLogin", "Button login control is null.");
            return false;
        end;

        local nTempID = TMailAccLogin.pBtnLogin:GetControlID();
        
        ui_utility.setproperty_by_id(nTempID, "OnClick", "TMailAccLogin.OnClkLogin");
        ui_utility.setproperty_by_id(nTempID, "OnMouseHover", "TMailAccLogin.OnLoginEnter");
        ui_utility.setproperty_by_id(nTempID, "OnMouseLeave", "TMailAccLogin.OnLoginLeave");

        -- 注册
        TMailAccLogin.pBtnRegister = ui_utility.get_ctrl_ptr_by_path(dwRoot, EMailAccLoginUI.sBtnRegister);
        if nil == TMailAccLogin.pBtnRegister then
            TGPLog.LogE("MailAccLogin", "Button Register login fail.");
            return false;
        end

        local nBtnRegister = TMailAccLogin.pBtnRegister:GetControlID();
        ui_utility.setproperty_by_id(nBtnRegister, "OnClick", "TMailAccLogin.OnClkBtnRegister");

        -- 忘记密码
        TMailAccLogin.pBtnForgetPSW = ui_utility.get_ctrl_ptr_by_path(dwRoot, EMailAccLoginUI.sBtnForgetPSW);
        if nil == TMailAccLogin.pBtnForgetPSW then
            TGPLog.LogE("MailAccLogin", "Button ForgetPSW login fail.");
            return false;
        end

        local nBtnForgetPSWID = TMailAccLogin.pBtnForgetPSW:GetControlID();
        ui_utility.setproperty_by_id(nBtnForgetPSWID, "OnClick", "TMailAccLogin.OnClkBtnForgetPSWID");

        if true == TMailAccLogin.bFirstShow then
            TMailAccLogin.SetTEditPwdMask(false);
            TMailAccLogin.bFirstShow = false;
        end

        TMailAccLogin.bPwdChange = false;

        if TMailAccLogin.CreateTEdit() then
            TMailAccLogin.ShowTEdit(true);
            return true;
        else
            TGPLog.LogE("MailAccLogin", "Create TEdit fail.");
            return false;
        end;
    end;

    -- 设置历史帐号列表
    SetAccList = function(zList)
        TMailAccLogin.zAccList = zList;
        TMailAccLogin.UpdateList();
    end;

    -- 邮箱是否保存密码
    IsSavedPwd = function(mailAcc)
        if nil == TMailAccLogin.zAccList then
            return false;
        end;

        for nIdx = 0, TMailAccLogin.zAccList.count - 1 do
            local   zItem = TMailAccLogin.zAccList.list[nIdx];

            if zItem.acc_name == mailAcc then
                return zItem.saved_pwd;
            end;
        end;

        return false;
    end;

    -- 点击用户登录
    OnClkLogin = function(pUILib, dwBtnID)
        TMailAccLogin.EnableFade(false);

        local   nOption = 0;
        local   bNetBar = TLoginModule.IsNetBar();

        if false == bNetBar then
            if nil ~= TMailAccLogin.pCbSavePwd and TRUE == TMailAccLogin.pCbSavePwd:IsChecked() then
                nOption = nOption + 1;
            end;

            if nil ~= TMailAccLogin.pCbAtLogin and TRUE == TMailAccLogin.pCbAtLogin:IsChecked() then
                nOption = nOption + 2;
            end;
        end;

        local   zParam = {};
        local   sInput = TMailAccLogin.GetCurMailAcc();

        if (nil == sInput) or ("" == sInput) or (string.len(sInput) < 4) then
            if nil ~= TMailAccLogin.pEdtUsrAcc then
                TMailAccLogin.pEdtUsrAcc:SetFocus();
            end;

            -- 显示一个错误提示
            local   zParam = {};

            zParam["err_msg" ] = LoadStr("TPF_LOGIN_ACCERR_TIPS");
            zParam["err_code"] = 7610006;
            comm_center.call_service("Svr_ShowLoginErr", zParam);

            TGPLog.LogI("MailAccLogin", "Account less than 4.");

            return;
        end;

        zParam["mail_acc"] = base_utility.trim(sInput);     -- 帐号
        zParam["opt"] = nOption;            -- Option
        zParam["type"] = ELoginType.ltMailLogin;

        -- 保存了密码
        if  (false == bNetBar) and TMailAccLogin.IsSavedPwd(sInput) then
            if TMailAccLogin.bPwdChange then
                if TLoginModule.HasTEdit() then     -- 安全的登录模式
                    zParam["sub_type"] = TLoginModule.MAIL_LOGIN_SUB_TYPE.kByEdit;      -- TEdit里面的密码登录
                else
                    zParam["sub_type"] = TLoginModule.MAIL_LOGIN_SUB_TYPE.kByPSW;
                    zParam["pwd"]  = TMailAccLogin.GetCurPwd();     -- 原始的帐号密码登录
                end;
            else
                zParam["sub_type"] = TLoginModule.MAIL_LOGIN_SUB_TYPE.kBySavedAcc; -- 保存的密码登录
            end;
        else
            if TLoginModule.HasTEdit() then
                zParam["sub_type"] = TLoginModule.MAIL_LOGIN_SUB_TYPE.kByEdit;          -- TEdit里面的密码登录
            else
                zParam["sub_type"] = TLoginModule.MAIL_LOGIN_SUB_TYPE.kByPSW;
                zParam["pwd"]  = TMailAccLogin.GetCurPwd();         -- 原始的帐号密码登录
            end;
        end;

        TMailAccLogin.sLastInput = sInput;
        comm_center.call_service("Svr_WGLogin", zParam);
    end;

    OnLoginEnter = function(pUILib, dwBtnID)
        TMailAccLogin.StartFade(false);
    end;

    OnLoginLeave = function(pUILib, dwBtnID)
        TMailAccLogin.StartFade(true);
    end;

    OnTimerFade = function()
        if not TMailAccLogin.bDoAnimate then
            TMailAccLogin.StopFade();
            return;
        end;

        local   nFadeStep = 50;

        if nil ~= TMailAccLogin.pPnlLight then
            local   bFinished = true;

            if TMailAccLogin.bIsFadeOut then
                TMailAccLogin.nFadeValue = TMailAccLogin.nFadeValue - nFadeStep;

                if TMailAccLogin.nFadeValue < 0 then
                    TMailAccLogin.nFadeValue = 0;
                else
                    bFinished = false;
                end;
            else
                TMailAccLogin.nFadeValue = TMailAccLogin.nFadeValue + nFadeStep;

                if TMailAccLogin.nFadeValue > 255 then
                    TMailAccLogin.nFadeValue = 255;
                else
                    bFinished = false;
                end;                
            end;

            ui_utility.setproperty_by_id(TMailAccLogin.pPnlLight:GetControlID(), "Alpha",
                                         tostring(TMailAccLogin.nFadeValue));

            if bFinished then
                TMailAccLogin.StopFade();
            end;
        else
            TGPLog.LogE("MailAccLogin", "FadeOut panel is null.");
        end;
    end;

    -- 开始淡入淡出
    StartFade = function(bFadeOut)
        if not TMailAccLogin.bDoAnimate then
            return;
        end;

        if TMailAccLogin.bIsFading and (bFadeOut == TMailAccLogin.bIsFadeOut) then
            return;
        end;

        if nil ~= TMailAccLogin.pPnlLight then
            TMailAccLogin.pPnlLight:Show(TRUE);
        end;

        if bFadeOut then
            TMailAccLogin.nFadeValue = 255;
        else
            TMailAccLogin.nFadeValue = 0;
        end;

        TMailAccLogin.bIsFadeOut = bFadeOut;

        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddInterval("TMailAccLogin.OnTimerFade", 20, 0);
        else
            TGPLog.LogE("MailAccLogin", "Start fade fail.");
        end;

        TMailAccLogin.bIsFading = true;
    end;

    -- 停止淡入淡出
    StopFade = function()
        if TMailAccLogin.bIsFading then
            local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveInterval("TMailAccLogin.OnTimerFade", 0);
            else
                TGPLog.LogE("MailAccLogin", "Stop fade fail");
            end;

            TMailAccLogin.bIsFading = false;
        end;

        if nil ~= TMailAccLogin.pPnlLight then
            if TMailAccLogin.bIsFadeOut then
                TMailAccLogin.pPnlLight:Show(FALSE);
            else
                ui_utility.setproperty_by_id(TMailAccLogin.pPnlLight:GetControlID(), "Alpha", "255");
            end;
        end;
    end;

    -- TIPS
    ShowTips = function(dwBtnID)
        if nil ~= TMailAccLogin.pNoQQTips then
            TMailAccLogin.pNoQQTips:Show(TRUE);
            TMailAccLogin.StartTimer();
        end;
    end;

    OnHideTips = function()
        if nil ~= TMailAccLogin.pNoQQTips then
            TMailAccLogin.pNoQQTips:Show(FALSE);
        end;
    end;

    StartTimer = function()
        if TMailAccLogin.bShowTips then
            return;
        end;

        local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddInterval("TMailAccLogin.OnHideTips", 2000, 0);
            TMailAccLogin.bShowTips = true;
        else
            TGPLog.LogE("MailAccLogin", "Start fade fail.");
        end;
    end;

    StopTimer = function()
        if TMailAccLogin.bShowTips then
            local pTPFUI = base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveInterval("TMailAccLogin.OnHideTips", 0);
                TMailAccLogin.bShowTips = false;
            else
                TGPLog.LogE("MailAccLogin", "Start fade fail.");
            end;
        end;
    end;

    -- 更新用户列表
    UpdateList = function()
        if not TAccComboBox.UpdateList(TMailAccLogin.zAccList) then
            TGPLog.LogI("MailAccLogin", "Update account list fail.");
            return;
        end;

        if (nil == TMailAccLogin.sLastInput or "" == TMailAccLogin.sLastInput) and 
           (TMailAccLogin.zAccList.count > 0) then
            TMailAccLogin.sLastInput = tostring(TMailAccLogin.zAccList.list[0].acc_name);
        end;

        if nil ~= TMailAccLogin.sLastInput and "" ~= TMailAccLogin.sLastInput then
            local   sText = U8toCUIStr(TMailAccLogin.sLastInput);
            TMailAccLogin.pEdtUsrAcc:SetText(sText);
            -- TMailAccLogin.SetAccEditTextTip();
            TMailAccLogin.OnMailAccChanged();
        end;
    end;

    -- 密码输入框
    OnPwdEdtChange = function()
        TMailAccLogin.bPwdChange = true;
        TGPLog.LogD("MailAccLogin", "Input edit text change.");
    end;

    -- 密码框
    OnAccKeyDown = function(pUILib, dwEdtID, dwVirtKey, dwKeyData)
        if 9 == dwVirtKey then      -- VK_TAB
            TMailAccLogin.OnTEditFocus();
        end;
    end;

    -- 输入的帐号变化
    OnEdtAccChange = function()
        TMailAccLogin.OnMailAccChanged();
    end;

    -- 编辑框
    OnAccSetFocus = function()
        TMailAccLogin.bAccFocus = true;

        if nil ~= TMailAccLogin.pLblAccTip then
            TMailAccLogin.pLblAccTip:Show(FALSE);
        end;
    end;

    OnAccKillFocus = function()
        TMailAccLogin.bAccFocus = false;
        TMailAccLogin.CheckAccTips();
    end;

    CheckAccTips = function()
        if nil ~= TMailAccLogin.pLblAccTip then
            if TMailAccLogin.bAccFocus then
                TMailAccLogin.pLblAccTip:Show(FALSE);
                return;
            end;

            local   sTemp = TMailAccLogin.GetCurMailAcc();

            if nil == sTemp or string.len(sTemp) <= 0 then
                TMailAccLogin.pLblAccTip:Show(TRUE);
            else
                TMailAccLogin.pLblAccTip:Show(FALSE);
            end;
        end;
    end;

    -- TEdit获得焦点
    OnTEditFocus = function()
        if TLoginModule.HasTEdit() then
            local   pLoginMgr = base_utility.get_component_adapter("ILogin_mgr",
                                "ierd_tgp.login_mgr.GetLoginMgr();");

            if nil == pLoginMgr then
                TGPLog.LogE("MailAccLogin", "Get login manager fail.");
                return;
            end;

            pLoginMgr:SetTeditFocus();
        else
            TGPLog.LogE("MailAccLogin", "TEdit get focus, edit is not created.")
        end;
    end;

    -- 帐号是否改变
    OnPwdChange = function(nEvtID, sJson)
        TMailAccLogin.bPwdChange = true;    -- 密码改变
    end;

    -- 帐号输入框设置焦点
    AccSetFocus = function()
        if nil ~= TMailAccLogin.pEdtUsrAcc then
            TMailAccLogin.pEdtUsrAcc:SetFocus();
        else
            TGPLog.LogE("MailAccLogin", "AccSetFocus, edit is null.");
        end;
    end;

    -- 检测状态
    CheckCapsStatus = function()
        -- 因为IsCapsLockOn函数不准确，使用内部变量来保存状态
        if nil == TMailAccLogin.bCapsLock then
            TMailAccLogin.bCapsLock = TMailAccLogin.IsCapsLockOn();
        else
            TMailAccLogin.bCapsLock = not TMailAccLogin.bCapsLock;
        end;

        TMailAccLogin.ShowCapsTips(TMailAccLogin.bCapsLock);
    end;

    -- 显示大写锁定状态提示
    OnPwdSetFocus = function()
        TMailAccLogin.bCapsLock = nil;
        TMailAccLogin.ShowCapsTips(true);
        if false == TMailAccLogin.CheckMailAccIsValid() then
            TMailAccLogin.ShowAccTips(true);
        end
    end;

    -- 隐藏大写锁定状态
    OnPwdKillFocus = function()
        TMailAccLogin.ShowCapsTips(false);
    end;

    -- 显示安全提示
    ShowTASTips = function(bShow)
        if bShow then
            TMailAccLogin.ShowToolTips(LoadStr("STR_LOGIN_ACCPROTECT_ENABLED"), TMailAccLogin.TASTIPSPOS);
        else
            comm_center.call_service("Svr_HideToolTips", {});
        end;
    end;

    ShowToolTips = function(sText, tPos)
        local   zParam = {};

        zParam["left"] = tPos.LEFT;
        zParam["top"]  = tPos.TOP;
        zParam["text"] = sText;

        comm_center.call_service("Svr_ShowToolTips", zParam);
    end;

    OnEnterPwdIcon = function()
        TMailAccLogin.ShowTASTips(true);
    end;

    OnLeavePwdIcon = function()
        TMailAccLogin.ShowTASTips(false);
    end;

    IsCapsLockOn = function()
        local   pLoginMgr = base_utility.get_component_adapter("ILogin_mgr",
                            "ierd_tgp.login_mgr.GetLoginMgr();");

        if nil == pLoginMgr then
            TGPLog.LogE("MailAccLogin", "CapsTips, Get login manager fail.");
            return;
        end;

        return pLoginMgr:IsCapsLockOn();
    end;

    -- 显示大写锁定提示
    ShowCapsTips = function(bShow)
        if bShow and TMailAccLogin.IsCapsLockOn() then
            TMailAccLogin.ShowToolTips(LoadStr("STR_LOGIN_CAPSLOCK_ON"), TMailAccLogin.TASTIPSPOS);
        else
            comm_center.call_service("Svr_HideToolTips", {});
        end;
    end;

    -- TAS消息
    OnTASMessage = function(nEvtID, sJson)
        local zParam = base_utility.json_to_table(sJson);

        if nil == zParam then
            TGPLog.LogE("MailAccLogin", "TASMessage, param is null.");
            return;
        end;

        local nMsgID = zParam.msp;
        local wParam = zParam.wparam;

        if 0x07 == nMsgID then              -- WM_SETFOCUS
            TMailAccLogin.OnPwdSetFocus();
        elseif 0x08 == nMsgID then          -- WM_KILLFOCUS
            TMailAccLogin.OnPwdKillFocus();
        elseif 0x104 == nMsgID then
            if 0x73 == wParam then
                comm_center.call_service("Svr_ExitWeGame", {});
            end;
        elseif 0x102 == nMsgID then  -- WM_CHAR
            if 0x9 == wParam then       -- VK_TAB
                TMailAccLogin.AccSetFocus();
            elseif 0x0d == wParam then  -- VK_RETURN
                TMailAccLogin.OnClkLogin(nil, 0);
            elseif 0x14 == wParam then  -- VK_CAPITAL
                TMailAccLogin.CheckCapsStatus();
            end
        end;
    end;

    -- 创建TEdit
    CreateTEdit = function()
        if TLoginModule.HasTEdit() then
            TMailAccLogin.RegTASEvent();
            TGPLog.LogI("LoginM", "TEdit already created.");
            return true;
        end;

        if (nil ~= TMailAccLogin.pEdtUsrPwd) then
            local   rtTemp = TMailAccLogin.pEdtUsrPwd:GetRectEx(TRUE,TRUE);

            if TLoginModule.CreateTEdit(rtTemp) then
                return true;
            else
                return false;
            end;
        else
            TGPLog.LogE("LoginV", "Create TEdit fail.");
            return false;
        end;
    end;

    -- 销毁TEdit
    DestroyTEdit = function()
        if TLoginModule.HasTEdit() then
            -- 反注册事件
            TMailAccLogin.UnRegTASEvent();

            -- 销毁EDIT
            TLoginModule.DestroyTEdit();
        else
            TGPLog.LogI("MailAccLogin", "TEdit already destroy.");
        end;
    end;

    RegTASEvent = function()
        if TMailAccLogin.bRegEvent then
            return;
        end;

        local   pTPFUI =  base_utility.get_component_adapter("ITpf_ui",
                            "ierd_tgp.tpf_ui.GetTPFUI();");

        if nil ~= pTPFUI then
            pTPFUI:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_MESSAGE, 
                                  "TMailAccLogin.OnTASMessage");
            pTPFUI:AddLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_PSW_CHANGED, 
                                  "TMailAccLogin.OnPwdChange");

            TMailAccLogin.bRegEvent = true;
        else
            TGPLog.LogE("MailAccLogin", "Get TPF UI fail.");
        end;
    end;

    UnRegTASEvent = function()
        if TMailAccLogin.bRegEvent then
            local   pTPFUI =  base_utility.get_component_adapter("ITpf_ui",
                                "ierd_tgp.tpf_ui.GetTPFUI();");

            if nil ~= pTPFUI then
                pTPFUI:RemoveLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_MESSAGE, 
                                         "TMailAccLogin.OnTASMessage");
                pTPFUI:RemoveLuaListener(ierd_tgp.login_mgr.LOGIN_ON_TAS_PSW_CHANGED, 
                                         "TMailAccLogin.OnPwdChange");
                TMailAccLogin.bRegEvent = false;
            else
                TGPLog.LogE("MailAccLogin", "Destroy TEdit, Get TPF UI fail.");
            end;
        end;
    end;

    -- 显示TEdit
    ShowTEdit = function(bShow)
        if TLoginModule.HasTEdit() then
            TLoginModule.ShowTEdit(bShow);

            if nil ~= TMailAccLogin.pEdtUsrPwd then
                if bShow then
                    TMailAccLogin.pEdtUsrPwd:Show(FALSE);
                else
                    TMailAccLogin.pEdtUsrPwd:Show(TRUE);
                end;
            end;
        else
            TGPLog.LogI("MailAccLogin", "TEdit not created.");
        end;
    end;

    -- 设置状态
    SetStatus = function(zParam)
    end;

    OnAccListShow = function(zParam)
        if 1 == zParam["show"] then
            TMailAccLogin.ShowTEdit(false);
        else
            TMailAccLogin.ShowTEdit(true);
        end;
    end;

    -- 点击消息处理
    OnClkAccItem = function(zParam)
        if nil ~= zParam and zParam.login_type == TLoginModule.LOGIN_ACCOUNT_TYPE.KMailLogin then
            TMailAccLogin.OnClkItem(ui_utility.uilib(), zParam["ctrl_id"], zParam["acc"]);
        else
            TGPLog.LogE("MailAccLogin", "Click item, param is null.");
        end;
    end;

    -- 帐号变化
    OnClkItem = function(pUILib, nBtnID, userData)
        if nil ~= pUILib and nil ~= nBtnID then
            local   pBtn = pUILib:GetControlPtr(nBtnID);

            if nil == pBtn then
                TGPLog.LogE("MailAccLogin", "Get account button fail.");
                return;
            end;

            if nil == TMailAccLogin.pEdtUsrAcc then
                TGPLog.LogE("MailAccLogin", "User account edit is null.");
                return;
            end;

            local   pTmp = tolua.cast(pBtn, "Tenio::ITButton");
            local   sUin = pTmp:GetCaption();
            if nil ~= userData and "" ~= userData then
                sUin = U8toCUIStr(userData);
            end

            if nil ~= sUin then
                TMailAccLogin.pEdtUsrAcc:SetText(sUin);
                -- TMailAccLogin.SetAccEditTextTip();
                TMailAccLogin.OnMailAccChanged();
            else
                TGPLog.LogE("MailAccLogin", "Get button text fail.");
            end;            
        else
            TGPLog.LogE("MailAccLogin", "Button click, UI lib is null.");
        end;
    end;

    -- 从密码输入框获取密码
    GetCurPwd = function()
        if nil ~= TMailAccLogin.pEdtUsrPwd then
            local   sText = TMailAccLogin.pEdtUsrPwd:GetText();

            if nil == sText then
                TGPLog.LogE("MailAccLogin", "Input password is null.");
                return nil;
            end;

            local   sTemp = CUIStrtoU8(sText);
            return  sTemp;
        else
            TGPLog.LogE("MailAccLogin", "Password edit is null.");
            return nil;
        end;
    end;

    -- 获取当前输入的邮箱账号
    GetCurMailAcc = function()
        if nil == TMailAccLogin.pEdtUsrAcc then
            TGPLog.LogE("MailAccLogin", "User account edit is null.");
            return nil;
        end;

        local   sMailAcc = TMailAccLogin.pEdtUsrAcc:GetText();

        if nil ~= sMailAcc then
            local   sTemp = CUIStrtoU8(sMailAcc);
            return sTemp;
        else
            return nil;
        end;
    end;

    -- 邮箱账号变化
    OnMailAccChanged = function()
        local   sMailAcc = TMailAccLogin.GetCurMailAcc();

        if (nil == sMailAcc) or ("" == sMailAcc) then
            if nil ~= TMailAccLogin.pCbSavePwd then
                TMailAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
            end;

            if nil ~= TMailAccLogin.pCbAtLogin then
                TMailAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
            end;
            TMailAccLogin.SetTEditPwdMask(false);

            ui_utility.setproperty_by_id(TMailAccLogin.nPnlUsrIcn, "Background", 
                                         EMailAccLoginUI.sDefIcon);

            TGPLog.LogI("MailAccLogin", "Get current mail acc fail.");

            if nil ~= TMailAccLogin.pLblAccTip then
                if TMailAccLogin.bAccFocus then
                    TMailAccLogin.pLblAccTip:Show(FALSE);
                else
                    TMailAccLogin.pLblAccTip:Show(TRUE);
                end;
            end;

            return;
        end;

        if nil == TMailAccLogin.zAccList then
            TGPLog.LogE("MailAccLogin", "Account list is null.");
            return;
        end;

        if nil == TMailAccLogin.pCbSavePwd then
            TGPLog.LogE("MailAccLogin", "Save password control is null.");
            return;
        end;

        if nil ~= TMailAccLogin.pLblAccTip then
            TMailAccLogin.pLblAccTip:Show(FALSE);
        end;

        -- local    nUin = tonumber(sMailAcc);
        local   bFind = false;

        for nIdx = 0, TMailAccLogin.zAccList.count - 1 do
            local   zTemp = TMailAccLogin.zAccList.list[nIdx];

            if nil ~= zTemp and zTemp.acc_name == sMailAcc then
                if TLoginModule.IsNetBar() then
                    TMailAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
                    TMailAccLogin.SetTEditPwdMask(false);

                    if nil ~= TMailAccLogin.pLblAccTip then
                        TMailAccLogin.pLblAccTip:Show(FALSE);
                    end;
                else
                    local strLog =  string.format("find save account, accout:%s, save_pwd:%s",
                        sMailAcc, tostring(zTemp.saved_pwd));
                    TGPLog.LogI("MailAccLogin", strLog);
                    if zTemp.saved_pwd then
                        TMailAccLogin.pCbSavePwd:SetCheckedNoSignal(TRUE);
                        TMailAccLogin.SetTEditPwdMask(true);
                    else
                        TMailAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
                        TMailAccLogin.SetTEditPwdMask(false);
                    end;
                end;

                -- 去掉自动登录标志
                if nil ~= TMailAccLogin.pCbAtLogin then
                    TMailAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
                end;

                if nil ~= zTemp.head and "" ~= zTemp.head then
                    ui_utility.setproperty_by_id(TMailAccLogin.nPnlUsrIcn, "Background", 
                                        "ONEIMAGE:" .. zTemp.head .. ";Stretch;0;");
                else
                    ui_utility.setproperty_by_id(TMailAccLogin.nPnlUsrIcn, "Background", 
                                         EMailAccLoginUI.sDefIcon);
                end;

                TMailAccLogin.bPwdChange = false;
                bFind = true;
                break;
            end;
        end;

        -- 没有保存的帐号，设置密码为空，自动登录、保存密码为未选中
        if not bFind then
            TGPLog.LogI("MailAccLogin", "not find save account info, account:" .. sMailAcc);
            TMailAccLogin.SetTEditPwdMask(false);

            if nil ~= TMailAccLogin.pCbSavePwd then
                TMailAccLogin.pCbSavePwd:SetCheckedNoSignal(FALSE);
            end;

            if nil ~= TMailAccLogin.pCbAtLogin then
                TMailAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
            end;

            ui_utility.setproperty_by_id(TMailAccLogin.nPnlUsrIcn, "Background", 
                                         EMailAccLoginUI.sDefIcon);
        end;
    end;

    -- 设置是否有保存密码标志
    SetTEditPwdMask = function(bMask)
        local   pLoginMgr = base_utility.get_component_adapter("ILogin_mgr",
                            "ierd_tgp.login_mgr.GetLoginMgr();");

        if nil == pLoginMgr then
            TGPLog.LogE("MailAccLogin", "Get login manager fail.");
            return;
        end;

        if TLoginModule.HasTEdit() then
            pLoginMgr:SetTeditPasswordMask(bMask);
            TGPLog.LogI("MailAccLogin", "Set TEdit password mask: " .. tostring(bMask));
        elseif nil ~= TMailAccLogin.pEdtUsrPwd then
            local   sTemp = nil;

            if bMask then
                sTemp = U8toCUIStr("1234567890");
            else
                sTemp = U8toCUIStr("");
            end;

            TMailAccLogin.pEdtUsrPwd:SetText(sTemp);
        end;
    end;

    OnSavePwdUnCheck = function()
        if nil ~= TMailAccLogin.pCbAtLogin then
            TMailAccLogin.pCbAtLogin:SetCheckedNoSignal(FALSE);
        end;
    end;

    OnAtLoginCheck = function()
        if nil ~= TMailAccLogin.pCbSavePwd then
            TMailAccLogin.pCbSavePwd:SetCheckedNoSignal(TRUE);
        end;
    end;

    -- 忘记密码
    OnClkBtnForgetPSWID = function (pUILib, nBtnID)
        if true == base_utility.is_develop_client() then
            web_utility.open_browser(LoadStr("LOGIN_FRAME_DEV_MAIL_RESET_PWD_URL"), false, true); 
            return;
        end
        web_utility.open_browser(LoadStr("LOGIN_FRAME_MAIL_RESET_PWD_URL"), false, true);
    end;

    -- 注册
    OnClkBtnRegister = function (pUILib, nBtnID)
        if true == base_utility.is_develop_client() then
            web_utility.open_browser(LoadStr("LOGIN_FRAME_DEV_MAIL_REISTER_URL"), false, true); 
            return;
        end
        web_utility.open_browser(LoadStr("LOGIN_FRAME_MAIL_REISTER_URL"), false, true); 
    end;

    SetAccEditTextTip = function ()
        if nil == TMailAccLogin.pEdtUsrAcc then
            return;
        end

        local   sMailAcc = TMailAccLogin.pEdtUsrAcc:GetText();

        if nil ~= sMailAcc then
            local sTemp = CUIStrtoU8(sMailAcc);
            if ui_utility.is_over_char_count(sTemp, TMailAccLogin.MAXCHAR) then
                ui_utility.setproperty_by_id(TMailAccLogin.nEdtUsrAcc, "TextTip", sTemp);
            end
        end
    end;

    -- 显示邮箱格式提示
    ShowAccTips = function(bShow)
        if bShow and TMailAccLogin.show_view == true then
            TMailAccLogin.ShowToolTips(LoadStr("STR_LOGIN_MAILACC_TIPS"), TMailAccLogin.ACCTIPSPOS);
        else
            comm_center.call_service("Svr_HideToolTips", {});
        end;
    end;

    CheckMailAccIsValid = function ()
        local sMailAcc = TMailAccLogin.GetCurMailAcc();
        local bValid = false;
        if string.match(sMailAcc, "[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?") then
            bValid = true;
        end
        return bValid;
    end;

    -- Private:
    zAccList   = nil;       -- 帐号列表
    bPwdChange = false;     -- 密码是否改变
    sLastInput = nil;       -- 最后一次输入的帐号
    pLblAccTip = nil;       -- 输入账号提示
    bAccFocus  = false;     -- 编辑框是否有焦点
    bRegEvent  = false;
    strCurMailAcc = "";

    -- 界面控件
    dwRootID   = 0;
    pPnlAccUI  = nil;       -- 帐号页面
    pEdtUsrPwd = nil;       -- 密码输入框
    nEdtUsrAcc = 0;
    nPnlUsrIcn = nil;       -- 用户图标

    pPnlLight  = nil;       -- 发光

    pNoQQTips  = nil;       -- 没有检测到QQ提示
    bShowTips  = false;

    nFadeValue = 0;
    bIsFadeOut = false;
    bDoAnimate = false;

    pCbSavePwd = nil;       -- 保存密码
    pCbAtLogin = nil;       -- 自动登录

--  pCapsTips  = nil;       -- 大写锁定控件
    bCapsLock  = nil;       -- 大写状态

    pBtnRegister = nil;     -- 注册
    pBtnLogin  = nil;       -- 登录
    pBtnForgetPSW   = nil;       -- 忘记密码
    nLblAccTip = 0;         -- tips
    MAXCHAR = 12;
    bFirstShow = true;
    TASTIPSPOS = {
        LEFT = 614,
        TOP = 198,
    };
    ACCTIPSPOS = {
        LEFT = 662,
        TOP = 168,
    };
};